package com.gaussian.property.module.quartz.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.gaussian.property.module.quartz.entity.SysJob;
import com.gaussian.property.module.quartz.enums.BusinessType;
import com.gaussian.property.module.quartz.service.ISysJobService;
import com.gaussian.property.module.quartz.util.CronUtils;
import com.songqi.common.core.domain.R;
import com.songqi.common.core.domain.page.PageDataInfo;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.exception.job.TaskException;
import com.songqi.common.log.annotation.Log;
import com.songqi.common.security.utils.SecurityUtils;
import com.songqi.common.web.request.controller.BaseController;
import org.apache.poi.ss.usermodel.Workbook;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 调度任务信息操作处理
 *
 * @author songqi(13776346982)
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController extends BaseController
{
    @Resource
    private ISysJobService jobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public R<PageDataInfo> list(SysJob sysJob)
    {
        startPage();
        List<SysJob> list = jobService.selectJobList(sysJob);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysJob sysJob)
    {
        List<SysJob> list = jobService.selectJobList(sysJob);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), SysJob.class, list);
        //TODO: 下载
        return R.success();
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobId}")
    public R getInfo(@PathVariable("jobId") Long jobId)
    {
        return R.success(jobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            //cron表达式不正确
            throw new CustomException("cron表达式不正确");
        }
        sysJob.setCreateBy(SecurityUtils.getUsername());
        return toAjax(jobService.insertJob(sysJob));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            //cron表达式不正确
            throw new CustomException("cron表达式不正确");
        }
        sysJob.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jobService.updateJob(sysJob));
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody SysJob job) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return toAjax(jobService.changeStatus(newJob));
    }


    /**
     * 修改定时任务是否记录日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeJobLog')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeJobLog")
    public R changeJobLog(@RequestBody SysJob job) throws SchedulerException, TaskException {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setJobLog(job.getJobLog());
        return toAjax(jobService.changeJobLog(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public R run(@RequestBody SysJob job) throws SchedulerException
    {
        jobService.run(job);
        return success();
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public R remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
    {
        jobService.deleteJobByIds(jobIds);
        return success();
    }
}
